<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Examples</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Chapter 1. Boost.JSON">
<link rel="up" href="../index.html" title="Chapter 1. Boost.JSON">
<link rel="prev" href="input_output.html" title="Input/Output">
<link rel="next" href="frequently_asked_questions.html" title="Frequently Asked Questions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="input_output.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="frequently_asked_questions.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="json.examples"></a><a class="link" href="examples.html" title="Examples">Examples</a>
</h2></div></div></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="json.examples.pretty"></a><a class="link" href="examples.html#json.examples.pretty" title="Pretty">Pretty</a>
</h3></div></div></div>
<pre class="programlisting"><span class="comment">/*
    This example parses a JSON file and pretty-prints
    it to standard output.
*/</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">json</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iomanip</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="string">"file.hpp"</span>

<span class="keyword">namespace</span> <span class="identifier">json</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">json</span><span class="special">;</span>

<span class="identifier">json</span><span class="special">::</span><span class="identifier">value</span>
<span class="identifier">parse_file</span><span class="special">(</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">filename</span> <span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">file</span> <span class="identifier">f</span><span class="special">(</span> <span class="identifier">filename</span><span class="special">,</span> <span class="string">"r"</span> <span class="special">);</span>
    <span class="identifier">json</span><span class="special">::</span><span class="identifier">stream_parser</span> <span class="identifier">p</span><span class="special">;</span>
    <span class="identifier">json</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">;</span>
    <span class="keyword">do</span>
    <span class="special">{</span>
        <span class="keyword">char</span> <span class="identifier">buf</span><span class="special">[</span><span class="number">4096</span><span class="special">];</span>
        <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">nread</span> <span class="special">=</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">read</span><span class="special">(</span> <span class="identifier">buf</span><span class="special">,</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">buf</span><span class="special">)</span> <span class="special">);</span>
        <span class="identifier">p</span><span class="special">.</span><span class="identifier">write</span><span class="special">(</span> <span class="identifier">buf</span><span class="special">,</span> <span class="identifier">nread</span><span class="special">,</span> <span class="identifier">ec</span> <span class="special">);</span>
    <span class="special">}</span>
    <span class="keyword">while</span><span class="special">(</span> <span class="special">!</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">eof</span><span class="special">()</span> <span class="special">);</span>
    <span class="keyword">if</span><span class="special">(</span> <span class="identifier">ec</span> <span class="special">)</span>
        <span class="keyword">return</span> <span class="keyword">nullptr</span><span class="special">;</span>
    <span class="identifier">p</span><span class="special">.</span><span class="identifier">finish</span><span class="special">(</span> <span class="identifier">ec</span> <span class="special">);</span>
    <span class="keyword">if</span><span class="special">(</span> <span class="identifier">ec</span> <span class="special">)</span>
        <span class="keyword">return</span> <span class="keyword">nullptr</span><span class="special">;</span>
    <span class="keyword">return</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">release</span><span class="special">();</span>
<span class="special">}</span>

<span class="keyword">void</span>
<span class="identifier">pretty_print</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="identifier">os</span><span class="special">,</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">value</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">jv</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">*</span> <span class="identifier">indent</span> <span class="special">=</span> <span class="keyword">nullptr</span> <span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">indent_</span><span class="special">;</span>
    <span class="keyword">if</span><span class="special">(!</span> <span class="identifier">indent</span><span class="special">)</span>
        <span class="identifier">indent</span> <span class="special">=</span> <span class="special">&amp;</span><span class="identifier">indent_</span><span class="special">;</span>
    <span class="keyword">switch</span><span class="special">(</span><span class="identifier">jv</span><span class="special">.</span><span class="identifier">kind</span><span class="special">())</span>
    <span class="special">{</span>
    <span class="keyword">case</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">kind</span><span class="special">::</span><span class="identifier">object</span><span class="special">:</span>
    <span class="special">{</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"{\n"</span><span class="special">;</span>
        <span class="identifier">indent</span><span class="special">-&gt;</span><span class="identifier">append</span><span class="special">(</span><span class="number">4</span><span class="special">,</span> <span class="char">' '</span><span class="special">);</span>
        <span class="keyword">auto</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">obj</span> <span class="special">=</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">get_object</span><span class="special">();</span>
        <span class="keyword">if</span><span class="special">(!</span> <span class="identifier">obj</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span>
        <span class="special">{</span>
            <span class="keyword">auto</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">obj</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
            <span class="keyword">for</span><span class="special">(;;)</span>
            <span class="special">{</span>
                <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">indent</span> <span class="special">&lt;&lt;</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">serialize</span><span class="special">(</span><span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">key</span><span class="special">())</span> <span class="special">&lt;&lt;</span> <span class="string">" : "</span><span class="special">;</span>
                <span class="identifier">pretty_print</span><span class="special">(</span><span class="identifier">os</span><span class="special">,</span> <span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">value</span><span class="special">(),</span> <span class="identifier">indent</span><span class="special">);</span>
                <span class="keyword">if</span><span class="special">(++</span><span class="identifier">it</span> <span class="special">==</span> <span class="identifier">obj</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span>
                    <span class="keyword">break</span><span class="special">;</span>
                <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">",\n"</span><span class="special">;</span>
            <span class="special">}</span>
        <span class="special">}</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="special">;</span>
        <span class="identifier">indent</span><span class="special">-&gt;</span><span class="identifier">resize</span><span class="special">(</span><span class="identifier">indent</span><span class="special">-&gt;</span><span class="identifier">size</span><span class="special">()</span> <span class="special">-</span> <span class="number">4</span><span class="special">);</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">indent</span> <span class="special">&lt;&lt;</span> <span class="string">"}"</span><span class="special">;</span>
        <span class="keyword">break</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">case</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">kind</span><span class="special">::</span><span class="identifier">array</span><span class="special">:</span>
    <span class="special">{</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"[\n"</span><span class="special">;</span>
        <span class="identifier">indent</span><span class="special">-&gt;</span><span class="identifier">append</span><span class="special">(</span><span class="number">4</span><span class="special">,</span> <span class="char">' '</span><span class="special">);</span>
        <span class="keyword">auto</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">arr</span> <span class="special">=</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">get_array</span><span class="special">();</span>
        <span class="keyword">if</span><span class="special">(!</span> <span class="identifier">arr</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span>
        <span class="special">{</span>
            <span class="keyword">auto</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">arr</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
            <span class="keyword">for</span><span class="special">(;;)</span>
            <span class="special">{</span>
                <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">indent</span><span class="special">;</span>
                <span class="identifier">pretty_print</span><span class="special">(</span> <span class="identifier">os</span><span class="special">,</span> <span class="special">*</span><span class="identifier">it</span><span class="special">,</span> <span class="identifier">indent</span><span class="special">);</span>
                <span class="keyword">if</span><span class="special">(++</span><span class="identifier">it</span> <span class="special">==</span> <span class="identifier">arr</span><span class="special">.</span><span class="identifier">end</span><span class="special">())</span>
                    <span class="keyword">break</span><span class="special">;</span>
                <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">",\n"</span><span class="special">;</span>
            <span class="special">}</span>
        <span class="special">}</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="special">;</span>
        <span class="identifier">indent</span><span class="special">-&gt;</span><span class="identifier">resize</span><span class="special">(</span><span class="identifier">indent</span><span class="special">-&gt;</span><span class="identifier">size</span><span class="special">()</span> <span class="special">-</span> <span class="number">4</span><span class="special">);</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">indent</span> <span class="special">&lt;&lt;</span> <span class="string">"]"</span><span class="special">;</span>
        <span class="keyword">break</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">case</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">kind</span><span class="special">::</span><span class="identifier">string</span><span class="special">:</span>
    <span class="special">{</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">serialize</span><span class="special">(</span><span class="identifier">jv</span><span class="special">.</span><span class="identifier">get_string</span><span class="special">());</span>
        <span class="keyword">break</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">case</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">kind</span><span class="special">::</span><span class="identifier">uint64</span><span class="special">:</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">get_uint64</span><span class="special">();</span>
        <span class="keyword">break</span><span class="special">;</span>

    <span class="keyword">case</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">kind</span><span class="special">::</span><span class="identifier">int64</span><span class="special">:</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">get_int64</span><span class="special">();</span>
        <span class="keyword">break</span><span class="special">;</span>

    <span class="keyword">case</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">kind</span><span class="special">::</span><span class="identifier">double_</span><span class="special">:</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">get_double</span><span class="special">();</span>
        <span class="keyword">break</span><span class="special">;</span>

    <span class="keyword">case</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">kind</span><span class="special">::</span><span class="identifier">bool_</span><span class="special">:</span>
        <span class="keyword">if</span><span class="special">(</span><span class="identifier">jv</span><span class="special">.</span><span class="identifier">get_bool</span><span class="special">())</span>
            <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"true"</span><span class="special">;</span>
        <span class="keyword">else</span>
            <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"false"</span><span class="special">;</span>
        <span class="keyword">break</span><span class="special">;</span>

    <span class="keyword">case</span> <span class="identifier">json</span><span class="special">::</span><span class="identifier">kind</span><span class="special">::</span><span class="identifier">null</span><span class="special">:</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"null"</span><span class="special">;</span>
        <span class="keyword">break</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">if</span><span class="special">(</span><span class="identifier">indent</span><span class="special">-&gt;</span><span class="identifier">empty</span><span class="special">())</span>
        <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">int</span>
<span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span><span class="special">**</span> <span class="identifier">argv</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">argc</span> <span class="special">!=</span> <span class="number">2</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cerr</span> <span class="special">&lt;&lt;</span>
            <span class="string">"Usage: pretty &lt;filename&gt;"</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="identifier">EXIT_FAILURE</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">try</span>
    <span class="special">{</span>
        <span class="comment">// Parse the file as JSON</span>
        <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">jv</span> <span class="special">=</span> <span class="identifier">parse_file</span><span class="special">(</span> <span class="identifier">argv</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">);</span>

        <span class="comment">// Now pretty-print the value</span>
        <span class="identifier">pretty_print</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">,</span> <span class="identifier">jv</span><span class="special">);</span>
    <span class="special">}</span>
    <span class="keyword">catch</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cerr</span> <span class="special">&lt;&lt;</span>
            <span class="string">"Caught exception: "</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="identifier">EXIT_FAILURE</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">return</span> <span class="identifier">EXIT_SUCCESS</span><span class="special">;</span>
<span class="special">}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="json.examples.validate"></a><a class="link" href="examples.html#json.examples.validate" title="Validate">Validate</a>
</h3></div></div></div>
<pre class="programlisting"><span class="comment">/*
    This example verifies that a file contains valid JSON.
*/</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">json</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="comment">// This file must be manually included when</span>
<span class="comment">// using basic_parser to implement a parser.</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">json</span><span class="special">/</span><span class="identifier">basic_parser_impl</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iomanip</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="string">"file.hpp"</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">json</span><span class="special">;</span>

<span class="comment">// The null parser discards all the data</span>
<span class="keyword">class</span> <span class="identifier">null_parser</span>
<span class="special">{</span>
    <span class="keyword">struct</span> <span class="identifier">handler</span>
    <span class="special">{</span>
        <span class="keyword">constexpr</span> <span class="keyword">static</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">max_object_size</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">(-</span><span class="number">1</span><span class="special">);</span>
        <span class="keyword">constexpr</span> <span class="keyword">static</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">max_array_size</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">(-</span><span class="number">1</span><span class="special">);</span>
        <span class="keyword">constexpr</span> <span class="keyword">static</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">max_key_size</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">(-</span><span class="number">1</span><span class="special">);</span>
        <span class="keyword">constexpr</span> <span class="keyword">static</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">max_string_size</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">(-</span><span class="number">1</span><span class="special">);</span>

        <span class="keyword">bool</span> <span class="identifier">on_document_begin</span><span class="special">(</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_document_end</span><span class="special">(</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_object_begin</span><span class="special">(</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_object_end</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_array_begin</span><span class="special">(</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_array_end</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_key_part</span><span class="special">(</span> <span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_key</span><span class="special">(</span> <span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_string_part</span><span class="special">(</span> <span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_string</span><span class="special">(</span> <span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_number_part</span><span class="special">(</span> <span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_int64</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">int64_t</span><span class="special">,</span> <span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_uint64</span><span class="special">(</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">uint64_t</span><span class="special">,</span> <span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_double</span><span class="special">(</span> <span class="keyword">double</span><span class="special">,</span> <span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_bool</span><span class="special">(</span> <span class="keyword">bool</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_null</span><span class="special">(</span> <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_comment_part</span><span class="special">(</span><span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
        <span class="keyword">bool</span> <span class="identifier">on_comment</span><span class="special">(</span><span class="identifier">string_view</span><span class="special">,</span> <span class="identifier">error_code</span><span class="special">&amp;)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span> <span class="special">}</span>
    <span class="special">};</span>

    <span class="identifier">basic_parser</span><span class="special">&lt;</span><span class="identifier">handler</span><span class="special">&gt;</span> <span class="identifier">p_</span><span class="special">;</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="identifier">null_parser</span><span class="special">()</span>
        <span class="special">:</span> <span class="identifier">p_</span><span class="special">(</span><span class="identifier">parse_options</span><span class="special">())</span>
    <span class="special">{</span>
    <span class="special">}</span>

    <span class="special">~</span><span class="identifier">null_parser</span><span class="special">()</span>
    <span class="special">{</span>
    <span class="special">}</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span>
    <span class="identifier">write</span><span class="special">(</span>
        <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">data</span><span class="special">,</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span><span class="special">,</span>
        <span class="identifier">error_code</span><span class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">n</span> <span class="special">=</span> <span class="identifier">p_</span><span class="special">.</span><span class="identifier">write_some</span><span class="special">(</span> <span class="keyword">false</span><span class="special">,</span> <span class="identifier">data</span><span class="special">,</span> <span class="identifier">size</span><span class="special">,</span> <span class="identifier">ec</span> <span class="special">);</span>
        <span class="keyword">if</span><span class="special">(!</span> <span class="identifier">ec</span> <span class="special">&amp;&amp;</span> <span class="identifier">n</span> <span class="special">&lt;</span> <span class="identifier">size</span><span class="special">)</span>
            <span class="identifier">ec</span> <span class="special">=</span> <span class="identifier">error</span><span class="special">::</span><span class="identifier">extra_data</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="identifier">n</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">bool</span>
<span class="identifier">validate</span><span class="special">(</span> <span class="identifier">string_view</span> <span class="identifier">s</span> <span class="special">)</span>
<span class="special">{</span>
    <span class="comment">// Parse with the null parser and return false on error</span>
    <span class="identifier">null_parser</span> <span class="identifier">p</span><span class="special">;</span>
    <span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">;</span>
    <span class="identifier">p</span><span class="special">.</span><span class="identifier">write</span><span class="special">(</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">size</span><span class="special">(),</span> <span class="identifier">ec</span> <span class="special">);</span>
    <span class="keyword">if</span><span class="special">(</span> <span class="identifier">ec</span> <span class="special">)</span>
        <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>

    <span class="comment">// The string is valid JSON.</span>
    <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
<span class="special">}</span>

<span class="keyword">int</span>
<span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span><span class="special">**</span> <span class="identifier">argv</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">argc</span> <span class="special">!=</span> <span class="number">2</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cerr</span> <span class="special">&lt;&lt;</span>
            <span class="string">"Usage: validate &lt;filename&gt;"</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="identifier">EXIT_FAILURE</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">try</span>
    <span class="special">{</span>
        <span class="comment">// Read the file into a string</span>
        <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">s</span> <span class="special">=</span> <span class="identifier">read_file</span><span class="special">(</span> <span class="identifier">argv</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">);</span>

        <span class="comment">// See if the string is valid JSON</span>
        <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">valid</span> <span class="special">=</span> <span class="identifier">validate</span><span class="special">(</span> <span class="identifier">s</span> <span class="special">);</span>

        <span class="comment">// Print the result</span>
        <span class="keyword">if</span><span class="special">(</span> <span class="identifier">valid</span> <span class="special">)</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">argv</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="string">" contains a valid JSON\n"</span><span class="special">;</span>
        <span class="keyword">else</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">argv</span><span class="special">[</span><span class="number">1</span><span class="special">]</span> <span class="special">&lt;&lt;</span> <span class="string">" does not contain a valid JSON\n"</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">catch</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cerr</span> <span class="special">&lt;&lt;</span>
            <span class="string">"Caught exception: "</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="keyword">return</span> <span class="identifier">EXIT_FAILURE</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">return</span> <span class="identifier">EXIT_SUCCESS</span><span class="special">;</span>
<span class="special">}</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2019, 2020 Vinnie Falco<br>Copyright © 2020 Krystian Stasiowski<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="input_output.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="frequently_asked_questions.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
